import shutil
import os

from pathlib import Path

import Utils

class EigenConfiguration:
    eigenVersion = "3.4.0"   
    eigenDirectory = "./Rasterizer/vendor/eigen3"
    eigenIncludeDirectory = "./Rasterizer/vendor/eigen3/eigen3/Eigen"
    eigenZipUrls = f"https://gitlab.com/libeigen/eigen/-/archive/{eigenVersion}/eigen-{eigenVersion}.zip"

    @classmethod
    def Validate(cls):
   
        if (cls.CheckEigen()):
           print("EigenSource已下载.")
           return False
       
        return True

    @classmethod
    def CheckEigen(cls):
        filepath = os.path.join(cls.eigenIncludeDirectory, "Eigen")
        print(filepath)
        if os.path.exists(filepath):
            return True
        else:
            cls.__DownloadEigen()
            return False
        
    @classmethod
    def __DownloadEigen(cls):
        permissionGranted = False
        while not permissionGranted:
            reply = str(input("请下载Eigen源码 {0:s}? [Y/N]: ".format(cls.eigenVersion))).lower().strip()[:1]
            if reply == 'n':
                return
            permissionGranted = (reply == 'y')
        
        eigenPath = f"{cls.eigenDirectory}/eigen-{cls.eigenVersion}.zip"
        print("Downloading {0:s} to {1:s}".format(cls.eigenZipUrls, eigenPath))
        Utils.DownloadFile(cls.eigenZipUrls, eigenPath)
        print("解压源文件", eigenPath)
        Utils.UnzipFile(eigenPath,deleteZipFile=True)
        cls.rename_directory("eigen3")
        print(f"eigen {cls.eigenVersion} 已经下载到 '{cls.eigenDirectory}'")

        return True
    

    @classmethod
    def rename_directory(cls, new_dir_name):
        # 构建旧目录的完整路径
        old_directory = os.path.join(cls.eigenDirectory, f"eigen-{cls.eigenVersion}")
        # 构建新目录的完整路径
        new_directory = os.path.join(cls.eigenDirectory, new_dir_name)

        try:
            os.rename(old_directory, new_directory)
            print(f"Renamed directory {old_directory} to {new_directory}")
        except OSError as e:
            print(f"Error renaming directory {old_directory}: {e}")


    
if __name__ == "__main__":
    EigenConfiguration.Validate()